<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" top="5vh" :close-on-click-modal='false' class="bonus">
    <div v-loading="loading">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="机构" name="org"></el-tab-pane>
        <el-tab-pane label="虚拟分组" name="groupTab"></el-tab-pane>
      </el-tabs>
      <!-- 机构-->
      <org-tree-by-dept v-show="activeName == 'org'" ref="orgTreeByDeptRef" @selectOrg="selectOrg" :height="450" :select="true" :arrow="false"></org-tree-by-dept>
      <!-- 虚拟分组 -->
      <virtual-group v-show="activeName == 'groupTab'" @selectGroup="selectGroup" ref="virtualGroupRef"></virtual-group>
      <div slot="footer" class="txt-right pd-t-10">
        <el-button @click="handleCancel(false)">取消</el-button>
        <el-button type="primary"  @click="handleSubmit">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import orgTreeByDept from "@/views/bonusmanage/components/orgTreeByDept";
import virtualGroup from "@/views/bonusmanage/components/virtualGroup";
export default {
  name: "addForm",
  components:{
    orgTreeByDept,
    virtualGroup
  },
  props:{
    loading:{
      type:Boolean,
      default:false,
    }
  },
  data(){
    return{
      type:'add',
      title:'选择机构/虚拟组',
      width:'30%',
      visible: false,
      activeName: 'org',
      formData: {bonusItemDeptId:''},
      orgList : [],
      groupList : []
    }
  },
  methods:{
    init(data,type){
      this.parentTableData = data || []
      this.visible = true ;
      this.initData(type) ;
    },
    initData(type){
      if (type == 'org'){
        this.activeName = 'org'
        this.$nextTick(()=>{
          this.$refs.orgTreeByDeptRef.getJiangJinJg(this.parentTableData) ;
        })
      }else{
        this.activeName = 'groupTab'
        this.$nextTick(()=>{
          this.$refs.virtualGroupRef.groupOnLoad(this.parentTableData) ;
        })
      }
    },
    handleSubmit(){
       if (this.activeName == 'org'){
          if (this.orgList.length == 0){
            this.$message.error("请选择机构")
            return
          }
          this.$emit('sendOrgGroupData',this.orgList,'org')
       }else{
         if (this.groupList.length == 0){
           this.$message.error("请选择虚拟组")
           return
         }
         this.$emit('sendOrgGroupData',this.groupList,'group')
       }

    },
    handleCancel(){

      this.activeName = 'org'
      this.orgList = [] ;
      this.groupList = [] ;
      this.visible = false ;
    },
    selectOrg(list){
      this.orgList = list ;
    },
    selectGroup(list){
      this.groupList = list ;
    },
    handleClick(tab, event) {
      this.initData(tab.name)
    },
  }
}
</script>

<style scoped>

</style>
